import axios from 'axios'

class LmwAxios {
    constructor(config) {
        this.instance = axios.create(config)
        this.interceptors = config.interceptors
        this.instance.interceptors.request.use(
            this.interceptors?.requestInterceptor,
            this.interceptors?.requestInterceptorCatch
        )
        this.instance.interceptors.response.use(
            this.interceptors?.responseInterceptor,
            this.interceptors?.responseInterceptorCatch
        )
        // 全局请求拦截
        this.instance.interceptors.request.use(
            (config) => {
                return config
            },
            (err) => {
                return err
            }
        )
        // 全局响应拦截
        this.instance.interceptors.response.use(
            (res) => {
                return Promise.resolve(res)
            },
            (err) => {
                return Promise.reject(err)
            }
        )
    }

    request(config) {
        return new Promise((resolve, reject) => {
            if (config.interceptors?.requestInterceptor) {
                config = config.interceptors.requestInterceptor(config)
            }
            this.instance
                .request(config)
                .then((res) => {
                    if (config.interceptors?.responseInterceptor) {
                        res = config.interceptors.responseInterceptor(res)
                    }
                    resolve(res)
                })
                .catch((err) => {
                    reject(err)
                })
        })
    }

    get(config) {
        return this.request({...config, method: 'GET'})
    }

    post(config) {
        return this.request({...config, method: 'POST'})
    }

    delete(config) {
        return this.request({...config, method: 'DELETE'})
    }

    patch(config) {
        return this.request({...config, method: 'PATCH'})
    }

    put(config) {
        return this.request({...config, method: 'PUT'})
    }
}

export default LmwAxios
